#include "CurveEvaluator.h"

float CurveEvaluator::s_fFlatnessEpsilon = 0.00001f;
int CurveEvaluator::s_iSegCount = 16;

CurveEvaluator::~CurveEvaluator(void)
{
}

double CurveEvaluator::evaluatePoint(double u, double u3_coeff, double u2_coeff, double u1_coeff, double u0_coeff) const{

	return (u0_coeff) + (u * u1_coeff) + (u * u * u2_coeff) + (u * u * u * u3_coeff);

}

double CurveEvaluator::evaluatePoint(double u, Vec4<double> coeffs) const{

	return evaluatePoint(u, coeffs[0], coeffs[1], coeffs[2], coeffs[3]);

}

vector<Point> CurveEvaluator::trimPoints(vector<Point> points) const{

	vector<Point> ret;
	double t = -0.000001;

	for (vector<Point>::iterator i = points.begin(); i != points.end(); i++) {
		if (i->x <= t) continue;
		t = i->x;
		ret.push_back(*i);
	}

	return ret;

}

vector<Point> CurveEvaluator::evaluateSpline(Mat4<double> basis, Vec4<double> ctrl_x, Vec4<double> ctrl_y, double sample_period) const{

	Vec4<double> coeffs_x = ctrl_x * basis;
	Vec4<double> coeffs_y = ctrl_y * basis;

	vector<Point> ret;
	for (double u = 0; u <= 1; u += sample_period) {
		double t_point = evaluatePoint(u, coeffs_x);
		double y_point = evaluatePoint(u, coeffs_y);

		ret.push_back(Point(t_point, y_point));
	}

	return trimPoints(ret);

}